package com.study.servlet;

import com.study.utils.ZXingCodeEncodeUtils;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

/**
 * 生成二维码的Servlet
 * @author Deja wu
 */

@WebServlet("/code")
public class CodeServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//        //生成图片
//        BufferedImage deja_wu = ZXingCodeEncodeUtils.createZXingCodeNormal("Deja wu", 400, 400);
//        ServletOutputStream outputStream = resp.getOutputStream();
//        ImageIO.write(deja_wu,"JPEG",outputStream);
//        outputStream.close();

        InputStream logoStream = this.getClass().getClassLoader().getResourceAsStream("logo.jpg");
        BufferedImage codeImage = ZXingCodeEncodeUtils.createZxingCodeUseLogo("王雪菲小仙女", 400, 400, logoStream);
        ServletOutputStream outputStream2 = resp.getOutputStream();
        ImageIO.write(codeImage,"JPEG",outputStream2);
        outputStream2.close();

    }
}
